package com.kzd.btgame.main.home.adapter

import android.os.Build
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.*
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.btgame.R
import com.kzd.btgame.callback.AppItemDecoration
import com.kzd.btgame.entity.*
import com.kzd.btgame.main.*
import com.kzd.btgame.main.game.adapter.*
import com.kzd.btgame.main.home.*
import com.module.lemlin.image.loadImage
import com.module.lemlin.image.loadRoundImage
import com.youth.banner.Banner

const val HOME_FEATURED_TYPE_2_N = -2
const val HOME_FEATURED_TYPE_1_N = -1
const val HOME_FEATURED_TYPE_0 = 0
const val HOME_FEATURED_TYPE_1 = 1
const val HOME_FEATURED_TYPE_2 = 2
const val HOME_FEATURED_TYPE_3 = 3
const val HOME_FEATURED_TYPE_4 = 4
const val HOME_FEATURED_TYPE_5 = 5
const val HOME_FEATURED_TYPE_10 = 10
const val HOME_FEATURED_TYPE_100 = 100

class HomeFeaturedAdapter : BaseMultiItemQuickAdapter<HomeIndex, BaseViewHolder>() {

    init {
        addItemType(HOME_FEATURED_TYPE_100, R.layout.view_home_title_banner)
        addItemType(HOME_FEATURED_TYPE_2_N, R.layout.view_home_title_recycle)
        addItemType(HOME_FEATURED_TYPE_1_N, R.layout.view_home_title_recycle)
        addItemType(HOME_FEATURED_TYPE_0, R.layout.view_home_title_image)
        addItemType(HOME_FEATURED_TYPE_1, R.layout.view_home_title_recycle)
        addItemType(HOME_FEATURED_TYPE_2, R.layout.view_home_title_recycle)
        addItemType(HOME_FEATURED_TYPE_3, R.layout.view_home_title_recycle)
        addItemType(HOME_FEATURED_TYPE_4, R.layout.view_home_title_banner)
        addItemType(HOME_FEATURED_TYPE_5, R.layout.view_home_title_recycle)
        addItemType(HOME_FEATURED_TYPE_10, R.layout.view_home_title_recycle)
    }

    override fun convert(holder: BaseViewHolder, item: HomeIndex) {

        when (holder.itemViewType) {
            HOME_FEATURED_TYPE_100 -> {
                holder.getView<LinearLayout>(R.id.llHomeTitleBannerName)
                    .visibility = View.GONE
                val banHomeTitleBannerBody =
                    holder.getView<Banner<*, *>>(R.id.banHomeTitleBannerBody)
                banHomeTitleBannerBody.setOrientation(Banner.HORIZONTAL)
                    .setUserInputEnabled(true)
                    .setBannerGalleryEffect((25), (15))
                    .adapter = Game4Adapter(item.getConversionList(GameBanner::class.java))
            }
            HOME_FEATURED_TYPE_2_N -> {
                holder.getView<LinearLayout>(R.id.llHomeTitleRecycleName)
                    .visibility = View.GONE
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    this.layoutManager = LinearLayoutManager(context).apply {
                        orientation = LinearLayoutManager.HORIZONTAL
                    }
                    addItemDecoration(AppItemDecoration())
                }.adapter = HomeNavAdapter().apply {
                    setNewInstance(item.getConversionList(HomeNav::class.java))
                    setOnItemClickListener { _, view, position ->
                        val itemData = getItem(position)
                        mOnItemHomeNav?.invoke(view, itemData)
                    }
                }
            }
            HOME_FEATURED_TYPE_1_N -> {
                holder.getView<LinearLayout>(R.id.llHomeTitleRecycleName)
                    .visibility = View.GONE
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    this.layoutManager = GridLayoutManager(context, 2)
                }.adapter = HomeAdvAdapter().apply {
                    setNewInstance(item.getConversionList(HomeAdv::class.java))
                }
            }
            HOME_FEATURED_TYPE_0 -> {
                holder.setText(R.id.tvHomeTitleImageName, item.title)
                    .getView<ImageView>(R.id.tvHomeTitleImageIcon)
                    .loadRoundImage(
                        src = item.img ?: R.mipmap.ic_80003,
                        ConvertUtils.dp2px((5f)), R.mipmap.ic_80003, R.mipmap.ic_80003
                    )
            }
            HOME_FEATURED_TYPE_1 -> {
                holder.setText(R.id.tvHomeTitleRecycleName, item.title)
                holder.getView<TextView>(R.id.tvHomeTitleRecycleMore)
                    .visibility = View.GONE
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    this.layoutManager = GridLayoutManager(context, 4)
                }.adapter = Game2Adapter().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
            HOME_FEATURED_TYPE_2 -> {
                holder.setText(R.id.tvHomeTitleRecycleName, item.title)
                holder.getView<TextView>(R.id.tvHomeTitleRecycleMore)
                    .visibility = View.GONE
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    this.layoutManager = LinearLayoutManager(context)
                }.adapter = Game1Adapter<GameBean>().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
            HOME_FEATURED_TYPE_3 -> {
                holder.setText(R.id.tvHomeTitleRecycleName, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    this.layoutManager = LinearLayoutManager(context).apply {
                        orientation = LinearLayoutManager.HORIZONTAL
                    }
                }.adapter = Game5Adapter().apply {
                    setNewInstance(item.getConversionList(GameStart::class.java))
                }
            }
            HOME_FEATURED_TYPE_4 -> {
                holder.setText(R.id.tvHomeTitleBannerName, item.title)
                val dataList = item.getConversionList(GameReserve::class.java)
                val banHomeTitleBannerBody =
                    holder.getView<Banner<GameReserve, Game6Adapter>>(R.id.banHomeTitleBannerBody)
                banHomeTitleBannerBody.setOrientation(Banner.HORIZONTAL)
                    .setUserInputEnabled(true)
                    .setBannerGalleryEffect((10), (10), (10))
                    .adapter = Game6Adapter(dataList).apply {
                    mGame6Adapter = this
                }
                LogUtils.e("============================asdfasdfasdf=================")
            }
            HOME_FEATURED_TYPE_5 -> {
                holder.setText(R.id.tvHomeTitleRecycleName, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    this.layoutManager = GridLayoutManager(context, 3)
                }.adapter = Game3Adapter().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
            HOME_FEATURED_TYPE_10 -> {
                holder.setText(R.id.tvHomeTitleRecycleName, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    this.layoutManager = GridLayoutManager(context, 4)
                }.adapter = HomeSortAdapter().apply {
                    setNewInstance(item.getConversionList(HomeSort::class.java))
                }
            }
        }
    }

    fun stopBannerViewSwipe() {
        for (value in findBannerViewMap().values) {
            value?.stop()
        }
    }

    fun startBannerViewSwipe() {
        for (value in findBannerViewMap().values) {
            value?.start()
        }
    }

    fun destroyBannerViewSwipe() {
        for (value in findBannerViewMap().values) {
            value?.destroy()
        }
    }

    private fun findBannerViewMap(): MutableMap<Int, Banner<*, *>?> {
        val map = mutableMapOf<Int, Banner<*, *>?>()
        data.filter {
            it.type == HOME_FEATURED_TYPE_100 || it.type == HOME_FEATURED_TYPE_4
        }.forEach {
            when (val view = getViewByPosition(getItemPosition(it), R.id.banHomeTitleBannerBody)) {
                null -> map[it.type] = null
                is Banner<*, *> -> map[it.type] = view
                else -> return@forEach
            }
        }
        return map
    }

    private var mGame6Adapter: Game6Adapter? = null

    fun setGame6AdapterData(position: Int, reserve: GameReserve) {
        mGame6Adapter?.setData(position, reserve)
    }

    private var mOnItemHomeNav: ((view: View, nav: HomeNav) -> Unit)? = null

    fun setOnItemHomeNav(listener: ((view: View, nav: HomeNav) -> Unit)) {
        this.mOnItemHomeNav = listener
    }
}

class HomeNavAdapter : BaseQuickAdapter<HomeNav, BaseViewHolder>(R.layout.item_home_nav) {
    override fun convert(holder: BaseViewHolder, item: HomeNav) {
        holder.setText(R.id.tvHomeNavName, item.name)
            .getView<ImageView>(R.id.ivHomeNavIcon).loadImage(item.icon)
    }
}

class HomeAdvAdapter :
    BaseQuickAdapter<HomeAdv, BaseViewHolder>(R.layout.item_home_adv) {
    override fun convert(holder: BaseViewHolder, item: HomeAdv) {
        holder.getView<ImageView>(R.id.ivHomeType1NIcon)
            .loadRoundImage(item.img, radius = ConvertUtils.dp2px(10f))
    }
}

class HomeSortAdapter :
    BaseQuickAdapter<HomeSort, BaseViewHolder>(R.layout.item_home_sort) {
    override fun convert(holder: BaseViewHolder, item: HomeSort) {
        holder.setText(R.id.tvHomeSortName, item.name)
    }
}

class HomeFabAdapter : BaseQuickAdapter<HomeFab, BaseViewHolder>(R.layout.item_home_fab) {

    private var onDownTimeListener: ((TextView, Int) -> Unit)? = null

    override fun convert(holder: BaseViewHolder, item: HomeFab) {
        holder.getView<ImageView>(R.id.ivHomeFabIcon)
            .loadImage(item.img ?: 0)
        holder.getView<TextView>(R.id.tvHomeFabTime).apply {
            visibility = if (item.end_time == 0) View.GONE else View.VISIBLE
            if (item.end_time != 0) {
                onDownTimeListener?.invoke(this, item.end_time)
            } else {
                text = "7天"
            }
        }
    }

    fun setOnDownTimeListener(onDownTimeListener: ((TextView, Int) -> Unit)) {
        this.onDownTimeListener = onDownTimeListener
    }
}


const val HOME_TRAVEL_TYPE_0 = 0
const val HOME_TRAVEL_TYPE_1 = 1
const val HOME_TRAVEL_TYPE_2 = 2
const val HOME_TRAVEL_TYPE_3 = 3

class HomeTravelAdapter : BaseMultiItemQuickAdapter<GameTravel, BaseViewHolder>() {

    init {
        addItemType(HOME_TRAVEL_TYPE_0, R.layout.view_home_title_banner)
        addItemType(HOME_TRAVEL_TYPE_1, R.layout.view_home_title_recycle)
        addItemType(HOME_TRAVEL_TYPE_2, R.layout.view_home_title_banner)
        addItemType(HOME_TRAVEL_TYPE_3, R.layout.view_home_title_recycle)
    }

    override fun convert(holder: BaseViewHolder, item: GameTravel) {
        when (holder.itemViewType) {
            HOME_TRAVEL_TYPE_0 -> {
                holder.getView<LinearLayout>(R.id.llHomeTitleBannerName)
                    .visibility = View.GONE
                val banHomeTitleBannerBody =
                    holder.getView<Banner<*, *>>(R.id.banHomeTitleBannerBody)
                banHomeTitleBannerBody.setOrientation(Banner.HORIZONTAL)
                    .setUserInputEnabled(true)
                    .setBannerGalleryEffect((25), (15))
                    .adapter = Game8Adapter(item.getConversionList(GameBean::class.java))
            }
            HOME_TRAVEL_TYPE_1 -> {
                holder.setText(R.id.tvHomeTitleRecycleName, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    layoutManager = LinearLayoutManager(context).apply {
                        orientation = LinearLayoutManager.VERTICAL
                    }
                }.adapter = Game9Adapter().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
            HOME_TRAVEL_TYPE_2 -> {
                holder.setText(R.id.tvHomeTitleBannerName, item.title)
                val dataList = item.getConversionList(GameReserve::class.java)
                val banHomeTitleBannerBody =
                    holder.getView<Banner<*, *>>(R.id.banHomeTitleBannerBody)
                banHomeTitleBannerBody.setOrientation(Banner.HORIZONTAL)
                    .setUserInputEnabled(true)
                    .setBannerGalleryEffect((10), (10), (10))
                    .adapter = Game6Adapter(dataList).apply {
                    mGame6Adapter = this
                }
            }
            HOME_TRAVEL_TYPE_3 -> {
                holder.setText(R.id.tvHomeTitleRecycleName, item.title)
                holder.getView<RecyclerView>(R.id.rvHomeTitleRecycleBody).apply {
                    layoutManager = LinearLayoutManager(context).apply {
                        orientation = LinearLayoutManager.VERTICAL
                    }
                }.adapter = Game9Adapter().apply {
                    setNewInstance(item.getConversionList(GameBean::class.java))
                }
            }
        }
    }

    fun stopBannerViewSwipe() {
        for (value in findBannerViewMap().values) {
            value?.stop()
        }
    }

    fun startBannerViewSwipe() {
        for (value in findBannerViewMap().values) {
            value?.start()
        }
    }

    fun destroyBannerViewSwipe() {
        for (value in findBannerViewMap().values) {
            value?.destroy()
        }
    }

    private fun findBannerViewMap(): MutableMap<Int, Banner<*, *>?> {
        val map = mutableMapOf<Int, Banner<*, *>?>()
        data.filter {
            it.type == HOME_TRAVEL_TYPE_0 || it.type == HOME_TRAVEL_TYPE_2
        }.forEach {
            when (val view = getViewByPosition(getItemPosition(it), R.id.banHomeTitleBannerBody)) {
                null -> map[it.type] = null
                is Banner<*, *> -> map[it.type] = view
                else -> return@forEach
            }
        }
        return map
    }

    private var mGame6Adapter: Game6Adapter? = null

    fun setGame6AdapterData(position: Int, reserve: GameReserve) {
        mGame6Adapter?.setData(position, reserve)
    }

}